package org.gwt.grin.client.util;

import org.gwt.grin.client.commands.Command;
import org.gwt.grin.client.features.InterpolatedModel;

public abstract class ShowBuilderHelper {

	protected InterpolatedModel makeFadeModel(String name, int[] frames,
			int[] fadeValues, int repeatFrame, int loopCount,
			Command[] endCommands) {
		int[][] values = new int[][] { fadeValues };
		return makeInterpolatedModel(name, frames, values, repeatFrame,
				loopCount, endCommands, false, false);
	}

	protected InterpolatedModel makeTimer(String name, int numFrames,
			boolean repeat, Command[] commands) {
		int[] frames = new int[] { 0, numFrames - 1 };
		// That means keyframes from 0 through numFrames-1, which is a total of
		// numFrames frames. For example, a timer that's one frame long runs
		// from frame 0 through frame 0.

		int[][] values = new int[0][];
		int repeatFrame = repeat ? 0 : Integer.MAX_VALUE;
		int loopCount = 1;

		/*
		 * Timer can be implemented as a degenerate case of InterpolateModel.
		 * It's just a model that interpolates zero data values between frame 0
		 * and frame numFrames.
		 */
		return makeInterpolatedModel(name, frames, values, repeatFrame,
				loopCount, commands, false, false);
	}

	protected InterpolatedModel makeTranslatorModel(String name, int[] frames,
			int[][] values, int repeatFrame, int loopCount,
			Command[] endCommands) {
		return makeInterpolatedModel(name, frames, values, repeatFrame,
				loopCount, endCommands, false, false);
	}

	protected InterpolatedModel makeScalingModel(String name, int[] frames,
			int[][] values, int repeatFrame, int loopCount, Command[] commands,
			boolean dynScaleX, boolean dynScaleY) {
		return makeInterpolatedModel(name, frames, values, repeatFrame,
				loopCount, commands, dynScaleX, dynScaleY);
	}

	protected InterpolatedModel makeInterpolatedModel(String name,
			int[] frames, int[][] values, int repeatFrame, int loopCount,
			Command[] endCommands, boolean dynScaleX, boolean dynScaleY) {
		int[] currValues = new int[values.length];
		for (int i = 0; i < values.length; i++) {
			int[] valueList = values[i];
			boolean allSame = true;
			currValues[i] = valueList[0];
			for (int j = 1; j < valueList.length; j++) {
				allSame = allSame && currValues[i] == valueList[j];
			}
			if (allSame) {
				values[i] = null;
				// if all of the values are the same, we don't need the list of
				// values for this parameter.
			}
		}
		InterpolatedModel result = new InterpolatedModel(name);
		result.setup(frames, currValues, values, repeatFrame, loopCount,
				endCommands, dynScaleX, dynScaleY);
		return result;
	}

}
